package com.atguigu.eduservice.controller;


import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.config.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wangshuai
 * @since 2021-01-26
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired  //注入service
    private EduTeacherService eduTeacherService;

    //查询所有讲师
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher() {
        List<EduTeacher> teachers = eduTeacherService.list(null);

        return R.ok().data("data",teachers);
    }

    //根据id逻辑删除讲师
    //@DeleteMapping("{id}")，id需要通过路径进行传递，http://localhost:8001/eduservice/teacher/findAll/1
    //@PathVariable String id,得到路径中的id值
    @ApiOperation(value = "根据id逻辑删除讲师")
    @GetMapping("{id}")
    public R removeteacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {

        boolean flag = eduTeacherService.removeById(Long.valueOf(id));

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //分页查询teacher
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{page}/{limit}")
    public R pageListTeacher(@PathVariable long page, @PathVariable long limit) {
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(page,limit);
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据封装到teacherPage
        IPage<EduTeacher> pageTeacher = eduTeacherService.page(teacherPage, null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
    }

    //条件查询带分页
    //RequestBody,使用json格式传递数据，把json数据封装到对应的对象中去。需要用post提交
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@PathVariable long page,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery
                                ) {
        Page<EduTeacher> teacherPage = new Page<>(page,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis动态拼接sql
        //判断是否为null或‘’，不是拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            //条件构造，模糊查询
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            //条件构造，模糊查询
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //条件构造，模糊查询
            //ge >= , gt >
            //数据库字段
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //条件构造，模糊查询
            //le >= , lt >
            //数据库字段
            wrapper.le("gmt_modified",end);
        }

        IPage<EduTeacher> pageTeacher = eduTeacherService.page(teacherPage, wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
    }

    //新增
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);

        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //修改
    //1.根据id查询
    @GetMapping("detail/{id}")
    public R teacherDeail(@PathVariable long id) {
        EduTeacher listById = eduTeacherService.getById(id);

        return R.ok().data("data",listById);
    }
    //2.修改
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //获取所有讲师
    @GetMapping("getAllTeacherList")
    public R getAllTeacherList() {

        List<EduTeacher> list = eduTeacherService.getAllTeacherList();

        return R.ok().data("list", list);
    }
}

